package com.starblink.feedback.adapter

import androidx.fragment.app.FragmentActivity
import com.starblink.android.basic.adapter.BaseBindingVH
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.basic.ext.load
import com.starblink.feedback.databinding.ItemPostProductBinding
import com.starblink.rocketreserver.fragment.ProductF

class PostProductHolder(binding: ItemPostProductBinding, val context: FragmentActivity) :
    BaseBindingVH<ProductF, ItemPostProductBinding>(binding = binding) {

    private val collectImg = com.starblink.basic.style.R.mipmap.ic_prd_collect
    private val collectedImg = com.starblink.basic.style.R.mipmap.ic_prd_collected

    override fun onBind(product: ProductF, position: Int) {
        product.mainImg?.let { binding.iv.load(it) }

        binding.tvName.text = product.title

        binding.groupPrice.visibleOrGone(product.price != null) {
            binding.tvPrice.text = product.price?.toString()
            binding.tvPriceSymbol1.gone()
            binding.tvPriceSymbol2.visible()
            binding.tvPriceSymbol2.text = product.currencySymbol
        }
        updateCollect(product)
    }

    private fun updateCollect(product: ProductF) {
        binding.imgCollect.setImageResource(product?.collected?.let {
            if (it) collectedImg else collectImg
        } ?: collectImg)
    }

}